import {
	mapState,
	mapActions
} from 'vuex'

export const tabPage = {
	computed: {
		...mapState(['vuex_tab_bar', 'vuex_module_page']),
		pageModuleName(currentUrl) {
			return (currentUrl) => {
				if (this.vuex_tab_bar.list.length > 0) {
					return this.vuex_tab_bar.list[this.tabIndex].url === currentUrl
				} else {
					return false
				}
			}
		}
	},
	data() {
		return {
			init: false
		}
	},
	async onLoad(options) {
		await this.$onLaunched;
		this.initLoad()
	},
	methods: {
		...mapActions(['loadTabBarList']),
		initLoad() {
			if (this.hasStart == 3) {
				this.startInit()
				return
			}
			if (!this.vuex_business.pk_id) {
				setTimeout(() => {
					this.initLoad()
				}, 300)
				return
			}
			this.$nextTick(() => {
				try {
					if (this.vuex_tab_bar.list.length === 0) {
						this.loadTabBarList().then(() => {
							this.$nextTick(() => {
								if (this.$refs.modulePageRef && this.$refs.modulePageRef
									.$onLoad) {
									this.$refs.modulePageRef.$onLoad()
								}
								if (this.$refs.modulePageRef && this.$refs.modulePageRef
									.$onShow) {
									this.$refs.modulePageRef.$onShow()
								}
							})
						})
					} else {
						this.$nextTick(() => {
							if (this.$refs.modulePageRef && this.$refs.modulePageRef
								.$onLoad) {
								this.$refs.modulePageRef.$onLoad()
							}
							if (this.$refs.modulePageRef && this.$refs.modulePageRef
								.$onShow) {
								this.$refs.modulePageRef.$onShow()
							}
						})
					}
				} catch (e) {
					console.log(e);
				}
			})
		},
	},
	onShow() {
		if (this.vuex_tab_bar.current !== this.tabIndex) {
			
			this.$store.commit('$uStore', {
				name: 'vuex_tab_bar.current',
				value: this.tabIndex
			})
		}

		if (this.init) {
			
			this.$nextTick(() => {
				try {
					if (this.$refs.modulePageRef && this.$refs.modulePageRef.$onShow) {
						this.$refs.modulePageRef.$onShow()
					}
				} catch (e) {

				}
			})
		} else {
			this.init = true
		}
	},
	onHide: function() {
		this.$nextTick(() => {
			try {
				if (this.$refs.modulePageRef.$onHide) {
					this.$refs.modulePageRef.$onHide()
				}
			} catch (e) {

			}
		})
	},
	onPullDownRefresh: function() {
		if (this.vuex_tab_bar.list.length === 0) {
			this.loadTabBarList()
		}
		if(this.hasStart == 3){return}
		try {
			if (this.$refs.modulePageRef.$onPullDownRefresh) {
				this.$refs.modulePageRef.$onPullDownRefresh()
			}
		} catch (e) {

		}
		setTimeout(() => {
			uni.stopPullDownRefresh()
		}, 700)
	},
	onReachBottom: function() {
		try {
			if (this.$refs.modulePageRef.$onReachBottom) {
				this.$refs.modulePageRef.$onReachBottom()
			}
		} catch (e) {

		}
	},
	onPageScroll: function(scroll) {
		try {
			if (this.$refs.modulePageRef.$onPageScroll) {
				this.$refs.modulePageRef.$onPageScroll(scroll)
			}
		} catch (e) {

		}
	},
	
	onShareAppMessage: function() {
		if (this.$refs.modulePageRef.getShareAppMessage) {
			return this.$refs.modulePageRef.getShareAppMessage()
		}
	},
	
	onShareTimeline: function() {
		if (this.$refs.modulePageRef.getShareTimeline) {
			return this.$refs.modulePageRef.getShareTimeline()
		}
	}
}
